 package market;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

 /**
  * 订单类
  */
 public class Order {
    private List<Fruit> fruits;
    private final SaleProxy saleProxy;

    public Order (SaleProxy saleProxy) {
        System.out.println("==================新订单=================");
        this.fruits = new ArrayList<>();
        this.saleProxy = saleProxy;
    }
    public void addFruits(Fruit fruit){
        this.fruits.add(fruit);
    }

    //计算订单总价
    public BigDecimal calculateTotalPrice(){
        BigDecimal orderTotalPrice = BigDecimal.ZERO;
        for (Fruit fruit : fruits) {
            //计算每个水果折扣后的价格
            BigDecimal price = fruit.getDiscountProxy().getAfterDiscountPrice(fruit.getPrice());
            BigDecimal fruitTotalPrice = price.multiply(BigDecimal.valueOf(fruit.getWeight()));
            System.out.printf("%s:%s斤 价格: %s元%n", fruit.getName(), fruit.getWeight(), fruitTotalPrice);
            orderTotalPrice = orderTotalPrice.add(fruitTotalPrice);
        }
        return this.saleProxy.getAfterSalePrice(orderTotalPrice);
    }


    public List<Fruit> getFruits(){
        return this.fruits;
    }

    public void setFruits(List<Fruit> fruits) {
        this.fruits = fruits;
    }
}
